home *** CD-ROM | disk | FTP | other *** search
- unit ComClientU;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls;
-
- type
- TForm1 = class(TForm)
- Button1: TButton;
- Button2: TButton;
- procedure Button1Click(Sender: TObject);
- procedure Button2Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- {$R *.DFM}
-
- uses
- BaseLib_TLB, CommonUnit, ActiveX, ComObj;
-
- var
- DllList: TStringList;
-
- function GetIFoo(const DllName: String): IFoo;
- var
- Idx: Integer;
- Dll: THandle;
- DllGetClassObject: function (const CLSID, IID: TGUID; var Obj): HResult; stdcall;
- FooFactory: IClassFactory;
- begin
- //Is DLL already in list?
- Idx := DllList.IndexOf(DllName);
- //If not, load it and add it
- if Idx = -1 then
- begin
- Dll := LoadLibrary(PChar(DllName));
- if Dll = 0 then
- RaiseLastWin32Error;
- DllList.AddObject(DllName, Pointer(Dll));
- end
- //else locate it
- else
- Dll := THandle(DllList.Objects[Idx]);
- //Find the key function
- DllGetClassObject := GetProcAddress(DLL, 'DllGetClassObject');
- if not Assigned(@DllGetClassObject) then
- RaiseLastWin32Error;
- //Call it to get the class factory
- OleCheck(DllGetClassObject(SharedClassID, IClassFactory, FooFactory));
- //Ask the class factory the COM object
- if Assigned(FooFactory) then
- OleCheck(FooFactory.CreateInstance(nil, IFoo, Result));
- end;
-
- procedure TidyUpDllList;
- var
- I: Integer;
- DllCanUnloadNow: function: HResult; stdcall;
- begin
- for I := 0 to DllList.Count - 1 do
- begin
- DllCanUnloadNow := GetProcAddress(THandle(DllList.Objects[0]), 'DllCanUnloadNow');
- if Assigned(@DllCanUnloadNow) and (DllCanUnloadNow = S_OK)then
- Freelibrary(THandle(DllList.Objects[0]));
- end;
- DllList.Free;
- DllList := nil
- end;
-
- procedure TForm1.Button1Click(Sender: TObject);
- var
- Foo: IFoo;
- begin
- Foo := GetIFoo('ComServer1.Dll');
- Foo.Bar;
- end;
-
- procedure TForm1.Button2Click(Sender: TObject);
- var
- Foo: IFoo;
- begin
- Foo := GetIFoo('ComServer2.Dll');
- Foo.Bar;
- end;
-
- initialization
- DllList := TStringList.Create;
- finalization
- TidyUpDllList
- end.
-